; $Id: synread.scm,v 1.2 1999/12/17 18:16:11 davidp Exp $
(load "printf.scm")
;
; The Following files are loaded for the Syntax command
;
; Find the index data for a given ACIS Scheme extension command
;
(define find-name (lambda (dataport command_name)
                    (define found 0)
                    (define out_list (list))
                    (let f ((x (read dataport)))
                      ; Search to the end of the file or found is equal to one.
                      (if (or (eof-object? x) (= found 1))
                          (begin
                            ; return a list of the found numbers.
                            out_list
                            )
                          (begin
                            ; Get the line number for this command
                            (define y (read dataport))
                            (if (string-ci=? command_name (symbol->string x))
                                (begin
                                  (define start 0)
                                  (set! start y)
                                  ; Get the next name
                                  (define next_name (read dataport))
                                  (define end 0)
                                  ; Get the next line number so we have the end of the command.
                                  (set! y (read dataport))
                                  (set! end y)
                                  ; Put these line numbers into a list so we can return them
                                  (set! out_list (list start))
                                  (set! out_list (append out_list (list end)))
                                  ; Set found to one so we stop the search
                                  (set! found 1)
                                  )
                                )
                            ; using tail recusion find the next command name.
                            (f (read dataport))
                            ))
                      )
                    ))

;
; Read a line from a file
;
(define (read-line . arg)
  (let* ((char (apply read-char arg)))
    (if (eof-object? char)
        char
        (do ((char char (apply read-char arg))
             (clist '() (cons char clist)))
            ((or (eof-object? char) (char=? #\newline char))
             (list->string (reverse clist)))))))
;
;Print out the syntax file from the star to end value
;
(define syntax-print (lambda (filename start_end)
                       (begin
                         ; Get the start and end line numbers
                         (define start (list-ref start_end 0))
                         (define end (list-ref start_end 1))
                         ; Open the Syntax data file.
                         (define dataport (open-input-file filename))
                         ; read to the start of the data
			 ; use read-string not read-line, it's MUCH faster,
			 ; conses much less too, and actually reads a whole line
			 (do ((count 1 (+ count 1))) ((= count start))
			   (read-string dataport))
                         ; Print the data out
                         (do ((count (- start 1) (+ count 1))) ((= count (- end 2)))
                             (define x (read-line dataport))
                             (if (not (eof-object? x))
                                 (begin
                                   (printf x)
                                   )
                                 )
                             )
                         
                         ; close data file
                         (close-input-port dataport)
                         )
                       ))
  ;
  ; Seach for a file with the load path
  ; return the file with the path if it is found
; Here is a usefull command
;
(define find-file
  (lambda ( in_file )
    (define name (string))
    (define found 0 )
    (begin
      ; Get the start and end line numbers
      (define path_list load-path)
      ; read to the start of the data
      (do ((count 0 (+ count 1))) ((or (= count (length path_list)) (= found 1)))
          (begin
            (define path (list-ref path_list count))
            (define temp (string-append path "/") )
            (define try_name (string-append temp in_file))
            (if (file-exists? try_name )
                (begin
                  (set! name try_name)
                  (set! found 1)
                  )
                )
            )
          )
      )
    name
    )
  )


